//
//  BChargeCashController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashController.h"

//
#import "BChargeCashTabBarController.h"

//view
#import "BChargeCashSegmentView.h"


@interface BChargeCashController ()

@property (nonatomic, strong) BChargeCashTabBarController *chargeCashTabBarController;

/**
 *BChargeCashSegmentView
 */
@property (nonatomic, strong) BChargeCashSegmentView *segmentView;

@end

@implementation BChargeCashController

- (BChargeCashTabBarController *)chargeCashTabBarController {
    if (!_chargeCashTabBarController) {
        _chargeCashTabBarController = [BChargeCashTabBarController new];
    }
    return _chargeCashTabBarController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    [self setupSubviews];
    //initset
    self.segmentView.segment.selectedSegmentIndex = 0;
    [self.segmentView layoutSelectBar:self.segmentView.segment];
    
    //
    [self addNotifications];
}
- (void)setupSubviews {
    [self.customNaviBar.leftButton setImage:nil forState:UIControlStateNormal];
    //
    self.segmentView = [BChargeCashSegmentView new];
    [self.customNaviBar addSubview:self.segmentView];
    [self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.customNaviBar);
        make.bottom.mas_equalTo(self.customNaviBar);
        make.height.mas_equalTo(44.0);
        make.width.mas_equalTo(200.0);
    }];
    [self.segmentView.segment addTarget:self action:@selector(segmentValueChangedAction:) forControlEvents:UIControlEventValueChanged];
    //
    self.chargeCashTabBarController.containerController = self.navigationController;
    [self.view addSubview:self.chargeCashTabBarController.view];
    [self.chargeCashTabBarController.view mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    //
    [self.view layoutIfNeeded];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //
    if (!self.chargeCashTabBarController.viewControllers.count) {
        self.segmentView.segment.selectedSegmentIndex = 0;
        [self.segmentView layoutSelectBar:self.segmentView.segment];
        [self.chargeCashTabBarController resetControllers];
    }
}

#pragma mark - action
#pragma mark - segmentValueChangedAction:
- (void)segmentValueChangedAction:(UISegmentedControl *)segment {
    [self.segmentView layoutSelectBar:segment];
    //选择controller
    self.chargeCashTabBarController.selectedIndex = segment.selectedSegmentIndex;
}


#pragma mark - 添加一些监听
- (void)addNotifications {
    //用户清空登录信息后需要重新设置充提币界面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userManagerClearLoginUserNotification:) name:BCUserManagerClearLoginUserNotification object:nil];
}


#pragma mark - userManagerClearLoginUserNotification:
- (void)userManagerClearLoginUserNotification:(NSNotification *)noti {
    self.chargeCashTabBarController.viewControllers = nil;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
